import { BrowserWindow, app, shell } from 'electron';
import icon from '../../resources/icon.png?asset'
import { join } from 'path'
import { is } from '@electron-toolkit/utils'

let playerWin = null;

const createplayerWin = () => {
    playerWin = new BrowserWindow({
        title: 'YangShaoTool MusicPlayer',
        width: 418,
        height: 70,
        maxHeight: 316,
        minHeight: 70,
        minWidth: 418,
        autoHideMenuBar: true,
        icon: icon,
        skipTaskbar: true, // 隐藏从任务栏中
        webPreferences: {
            nodeIntegration: true,
            preload: join(__dirname, '../preload/index.js'),
            webSecurity: false, // 允许加载本地资源
            // 添加以下配置以支持点击穿透
            webviewTag: true,
            additionalArguments: ['--enable-transparent-visuals', '--disable-gpu']
        },
        resizable: true,
        show: false,
        transparent: true, // 设置窗口透明
        frame: false, // 如果想要无边框的窗口
    });
    const screenSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
    playerWin.setPosition(screenSize.width / 2 - 200, 0); // 顶部居中
    // 保持在屏幕最上层--start
    playerWin.setAlwaysOnTop(true, "screen-saver")
    playerWin.setVisibleOnAllWorkspaces(true)
    // --end
    if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
        playerWin.loadURL(process.env['ELECTRON_RENDERER_URL'])
    } else {
        playerWin.loadFile(join(__dirname, '../renderer/index.html'))
    }
    // 假设你想在创建窗口后立即打开设置路由
    playerWin.webContents.on('did-finish-load', () => {
        playerWin.webContents.send('set-route', '/musicPlayer');
    });
    playerWin.webContents.setWindowOpenHandler((details) => {
        if (url.startsWith('https:')) shell.openExternal(url);
        return { action: 'deny' }
    })

    return playerWin;
}
function closeplayerWin() {
    if (playerWin && !playerWin?.isDestroyed()) {
        playerWin?.close();
    }
    playerWin = null;
}

function openplayerWin() {
    if (!playerWin || playerWin?.isDestroyed()) {
        playerWin = createplayerWin();
    }
}
function showplayerWin() {
    playerWin?.show();
}

function hideplayerWin() {
    playerWin.hide();
}
// 写一个show和hide切换
function showHideplayerWin() {
    playerWin.isVisible() ? playerWin.hide() : playerWin.show();
}
function goHomeplayerWin() {
    playerWin.loadURL('http://localhost:3456/musicPlayer')
}

function minimizeplayerWin() {
    playerWin.minimize();
}

function focusplayerWin() {
    if (!playerWin || playerWin?.isDestroyed()) {
        playerWin = createplayerWin();
    } else {
        // Focus on the main window if the user tried to open another
        if (playerWin.isMinimized()) playerWin.restore();
        if (!playerWin.isVisible()) playerWin.show();
        playerWin.focus();
    }
}

function sendEuUpdateCanAvailable(arg, update) {
    if (playerWin && !playerWin?.isDestroyed()) {
        playerWin.webContents.send('eu:update-can-available', {
            update: update,
            version: app.getVersion(),
            newVersion: arg?.version,
        });
    }
}
function setListShow() {
    playerWin.setSize(418, 316);
    let bounds = playerWin.getBounds();
}
function setListHide() {
    playerWin.setSize(418, 70);
    let bounds = playerWin.getBounds();
}

export {
    closeplayerWin,
    createplayerWin,
    showplayerWin,
    focusplayerWin,
    hideplayerWin,
    minimizeplayerWin,
    openplayerWin,
    sendEuUpdateCanAvailable,
    showHideplayerWin,
    goHomeplayerWin,
    setListShow,
    setListHide
};